﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using GroundWater.Data;
using GroundWater.BusinessActions;

using SectionPlaneTable = GroundWater.Data.InfoDataSet.SectionPlaneDataTable;
using SectionPlaneRow = GroundWater.Data.InfoDataSet.SectionPlaneRow;

namespace GroundWater.Basedata
{
    public partial class SectionPlane : PageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            SiteUtil.Validate(this, "剖面信息维护");
        }

        protected void RowDataBound(object sender, GridViewRowEventArgs args)
        {
            GridView gridView = (GridView)sender;

            if (gridView.SortExpression.Length > 0)
            {
                int cellIndex = -1;
                foreach (DataControlField field in gridView.Columns)
                {
                    if (field.SortExpression == gridView.SortExpression)
                    {
                        cellIndex = gridView.Columns.IndexOf(field);
                        break;
                    }
                }

                if (cellIndex > -1)
                {
                    if (args.Row.RowType == DataControlRowType.Header)
                    {
                        //  this is a header row,
                        //  set the sort style
                        args.Row.Cells[cellIndex].CssClass +=
                            (gridView.SortDirection == SortDirection.Ascending
                            ? " sortasc" : " sortdesc");
                    }
                }
            }

            if (args.Row.RowType == DataControlRowType.DataRow)
            {
                if (args.Row.RowIndex > -1)
                {
                    if (args.Row.Cells[4].Controls.Count > 0)
                    {
                        LinkButton btnDelete = args.Row.Cells[4].Controls[0] as LinkButton;
                        if (btnDelete != null && btnDelete.Text == "删除")
                        {
                            btnDelete.OnClientClick = "return confirm('您是否确认删除此项？');";
                        }
                    }
                }
            }
        }

        protected void ods_Deleting(object sender, ObjectDataSourceMethodEventArgs e)
        {
            object id = e.InputParameters["id"];
            //e.InputParameters["id"] = new Guid((byte[])e.InputParameters["id"]);
            //e.InputParameters.Remove("Code");
        }

        protected void btnEdit_Click(object sender, EventArgs e)
        {
            GridViewRow row = ((LinkButton)sender).Parent.Parent as GridViewRow;
            byte[] userId = (byte[])this.gv.DataKeys[row.RowIndex].Value;
            SetFields(userId);

            updPnl.Update();
            //txtCode.Enabled = false;

            mdlPopup.Show();
        }

        private void SetFields(byte[] id)
        {
            if (id != null)
            {
                SectionPlaneRow row = GroundWater.BusinessActions.SectionPlane.GetById(id, false);
                txtCode.Text = row.Code;
                txtName.Text = row.Name;
                if (!row.IsDescriptionNull())
                {
                    txtDescription.Text = row.Description;
                }

                

                this.ViewState["WaterSourceId"] = id;
            }
        }

        protected void btnOK_Click(object sender, EventArgs e)
        {
            if (txtCode.Text.Trim().Length == 0 || txtName.Text.Length == 0) return;
            byte[] id = this.ViewState["WaterSourceId"] as byte[];



            InfoDataSet s = GetFields(id);
            if (s == null)
            {
                //
                sm.RegisterDataItem(txtName, "1");
                lblJavaScript.Text = "<script type=\"text/javascript\">alert('已存在名称为" + txtCode.Text.Trim() + " 的剖面。');</script>";
                return;
            }

            GroundWater.BusinessActions.SectionPlane.Update(s, false, true, new string[] { s.SectionPlane.TableName });

            gv.DataBind();

            updPanel.Update();
            this.ViewState.Remove("WaterSourceId");

            ClearFields();
        }

        private void ClearFields()
        {
            txtCode.Text = "";
            txtName.Text = "";
            txtDescription.Text = "";
            
        }

        private InfoDataSet GetFields(byte[] id)
        {
            SectionPlaneRow src = null;

            if (id == null)
            {
                //SectionPlaneRow row = GroundWater.BusinessActions.SectionPlane.GetById(id, false);
                SectionPlaneTable dt = GroundWater.BusinessActions.SectionPlane.GetAll(false, true);
                var rows = dt.AsEnumerable();
                SectionPlaneRow row = rows.SingleOrDefault(r => r.Name == txtName.Text.Trim());

                if (row != null)
                {
                    return null;
                }

                src = (new InfoDataSet()).SectionPlane.NewSectionPlaneRow();
                src.Id = Guid.NewGuid().ToByteArray();
                src.Code = "";
                src.Name = "";
                src.Description = "";
            }
            else
            {
                // update
                src = GroundWater.BusinessActions.SectionPlane.GetById(id, false);
            }
            
            src.Code = txtCode.Text;
            src.Name = txtName.Text;
            src.QueryCode = Husb.Util.CommonUtil.GetChineseSpell(txtName.Text);
            src.Description = txtDescription.Text;


            if (id == null)
            {
                src.Table.Rows.Add(src);
            }
            else
            {

            }

            return src.Table.DataSet as InfoDataSet;
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            //txtCode.Enabled = true;

            ClearFields();
            mdlPopup.Show();
        }
    }
}
